package com.esun.payment_api.controller;


import com.esun.common.api.CommonPage;
import com.esun.common.api.CommonResult;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.domain.DkCusSubs;
import com.esun.payment_api.domain.PinYin;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.domain.WldwList;
import com.esun.payment_api.dto.DkCusSubsQueryParam;
import com.esun.payment_api.dto.WldwQueryParam;
import com.esun.payment_api.service.DkCusSubsService;
import com.esun.payment_api.service.WldwListService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/cus_subs")
public class DkCusSubsController extends BaseController {

    @Resource
    private DkCusSubsService dkCusSubsService;

    @Resource
    private WldwListService wldwListService;

    //往来单位已订阅列表
    @GetMapping("/list")
    public CommonResult<CommonPage<DkCusSubs>> getList(Integer dept_id, String searchStr,
                                                       @RequestParam(value = "co_id", defaultValue = "22") Integer co_id,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        List<DkCusSubs> list = dkCusSubsService.getList(dept_id, searchStr, co_id, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    //往来单位未订阅列表
    @GetMapping("/wldw")
    public CommonResult<CommonPage<WldwList>> getList(WldwQueryParam wldwQueryParam,
                                                      @RequestParam(value = "co_id", defaultValue = "22") Integer co_id,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            return CommonResult.failed("无法从token中获取用户信息");
        }
        List<WldwList> list = wldwListService.getList(wldwQueryParam, co_id, Integer.valueOf(user.getDeptId()), pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    //往来单位已订阅数量
    @RequestMapping("/getSubscribeNumber")
    public CommonResult getSubscribeNumber() {
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取用户信息");
        }
        Integer size = dkCusSubsService.getSubscribeNumber(user);

        return CommonResult.success(size, "获取成功，往来单位订阅数量为" + size);
    }

    //往来单位--订阅
    @RequestMapping("/customerSubscribe")
    public CommonResult customerSubscribe(Integer cusId) {
        if (BlankUtils.isBlank(cusId)) {
            return CommonResult.failed("往来单位id不能为空");
        }
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取用户信息");
        }
        dkCusSubsService.customerSubscribe(cusId, user);
        return CommonResult.success("操作成功，已订阅该单位");
    }

    //往来单位--取消订阅
    @RequestMapping("/customerUnsubscribe")
    public CommonResult customerUnsubscribe(Integer cusId) {
        if (BlankUtils.isBlank(cusId)) {
            return CommonResult.failed("往来单位id不能为空");
        }
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取用户信息");
        }
        dkCusSubsService.customerUnsubscribe(cusId, user);
        return CommonResult.success("操作成功，已取消订阅该单位");
    }
}

